<?php

namespace App\Repositories;
use Cache;
use App\Models\Category;
use App\Repositories\Repository;

class CategoryRepository extends Repository{

    static $tag = 'testing.category';

    public function store($data)
    {
        $res = Category::firstOrCreate($data);
        return $res;
    }

    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.cache_key.category.info');
        $cate = $this->remember($cacheKey.$id, function () use ($id) {
            return Category::where('id', $id)->firstOrFail();
        });
        return $cate;
    }

    public function getList(){
        $key = getCacheKey('testing.cache_key.category.list');
        $list = $this->remember($key,function(){
            return Category::all();
        });
        return $list;
    }

    public function getCateOptions(){
        $list = Category::all();
        $data = [];
        if($list){
            $obj = array();
            foreach ($list as $key => $val){
                $obj['id'] = $val['id'];
                $obj['text'] = $val['name'];
                array_push($data,$obj);
            }
        }
        return $data;
    }


    public function model()
    {
        return app(Category::class);
    }

    public function tag()
    {
        return self::$tag;
    }

    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }
}